import api from '../../utils/api'
Page({
  data: {
    dataForm: {
      // operator: '',
      // userCode: '',
      // inviteCode: '',
      matNo: '',
      carNo: '',
      occupy: false,
      carColor: '',
      carColorName: '',
      carType: '',
      carTypeName: '',
      applyEmpName: '',
      applyEmpPhone: '',
      inValidStime: '',
      outValidEtime: '',
      inFactPlace: '',
      inFactTask: '原板进厂',
      field4: 'JFE钢板有限公司',
      forwardUnit: '原板',
      remark: '',
    },
    id: '',
    isSubmitting: false,
    matNoList: []
  },
  onShow() {
    const searchRes = wx.getStorageSync("searchRes");
    const index = wx.getStorageSync("matNoIndex");
    if (searchRes) {
      wx.removeStorageSync("searchRes");
      wx.removeStorageSync("matNoIndex");
      this.data.matNoList[index].matNo = searchRes.value
      console.log(this.data.matNoList)
      this.setData({
        matNoList: this.data.matNoList
      })
    }
  },
  onLoad: async function (options) {
    const user = wx.getStorageSync('user');
    await api.getByOpenId({
      openId: user.openid
    }).then(res => {
      this.data.dataForm.carNo = res.data.carNo
      this.setData({
        dataForm: this.data.dataForm
      })
    })
    this.setData({
      "dataForm.operator": user.iphone
    })
    const id = options.id;
    if (id) {
      this.setData({
        id: id
      })
      const data = await api.carOrderDetials({
        id: id
      })
      const dataLIST = data.data
      for (let key in dataLIST) {
        this.data.dataForm[key] = dataLIST[key];
      }

      this.setData({
        dataForm: this.data.dataForm
      })
      await api.getDictData({
        dictCode: 'car_color'
      }).then(res => {
        res.data.forEach(item => {
          if (item.dictValue == this.data.dataForm.carColor) {
            this.data.dataForm.carColorName = item.dictName
          }
        });
      })
      await api.getDictData({
        dictCode: 'TRUCK_TYPE'
      }).then(res => {
        res.data.forEach(item => {
          if (item.dictValue == this.data.dataForm.carType) {
            this.data.dataForm.carTypeName = item.dictName
          }
        });
      })
      this.setData({
        dataForm: this.data.dataForm
      })
    }



  },
  noInput13: function (e) {
    this.data.dataForm.userCode = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  noInput2: function (e) {
    this.data.dataForm.inviteCode = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  noInput: function (e) {
    this.data.dataForm.carNo = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  qrcode(e) {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['barCode'],
      success: (res) => {
        console.log(res, 111)
        this.matNoAdd(res.result)

        // this.data.dataForm.matNo = res.result
        // this.setData({
        //   dataForm: this.data.dataForm
        // })
      }
    })
  },
  searchCar(e) {
    const index = e.currentTarget.dataset.index;
    this.data.matNoList[index].matNo = e.detail.value
    console.log(this.data.matNoList)
    this.setData({
      matNoList: this.data.matNoList
    })

  },
  getAdd() {

    let arr = [],
      matNo = ''
    this.data.matNoList.forEach(item => {
      if (item.matNo) {
        arr.push(item.matNo)
      }
    });
    if (arr.length > 0) {
      this.data.dataForm.matNo = arr.join(',')
      matNo = arr.join(',')
      this.setData({
        dataForm: this.data.dataForm
      })
      api.getOccupy({
        matNo: matNo
      }).then(res => {
        this.setData({
          'dataForm.occupy': res.data.occupy
        })
        if (res.data.occupy) {
          if (!this.data.dataForm.carNo.trim()) {
            wx.showToast({
              title: '车辆号不能为空',
              icon: 'none',
            });
            return;
          }
          if (this.data.isSubmitting) return; // 如果正在提交，则不再处理新的提交请求
          // 设置为正在提交状态
          this.setData({
            isSubmitting: true
          });
          wx.showLoading({
            title: '提交中...',
          });
          api.rowBoardCarAdd(this.data.dataForm).then(res => {
            setTimeout(function () {
              wx.hideLoading();
              wx.redirectTo({
                url: '/pages/originalList/original', //附带两个参数过去
              })
              // this.setData({
              //   isSubmitting: false
              // }); // 请求成功后重置提交状态

            }, 3000)

          }).catch(e => {
            this.setData({
              isSubmitting: false
            }); // 请求失败后重置提交状态
          })
        }
        if (!res.data.occupy) {
          wx.showToast({
            title: '钢卷号不可用',
            icon: 'none',
          });
        }


      })
    } else {
      wx.showToast({
        title: '钢卷号不得为空',
        icon: 'none',
      });

    }

    // const {
    //   matNo,
    //   carNo,
    //   occupy,
    //   // userCode,
    //   // inviteCode
    // } = this.data.dataForm
    // if (!matNo.trim()) {
    //   wx.showToast({
    //     title: '钢卷号为空/或未进行查询',
    //     icon: 'none',
    //   });
    //   return;
    // }
    // if (!inviteCode.trim()) {
    //   wx.showToast({
    //     title: '邀请码不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    // if (!userCode.trim()) {
    //   wx.showToast({
    //     title: '被访人姓名不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    // if (!carNo.trim()) {
    //   wx.showToast({
    //     title: '车辆号不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }

    // if (!occupy) {
    //   wx.showToast({
    //     title: '钢卷号不可用或',
    //     icon: 'none',
    //   });
    //   return;
    // }

  },
  showList() {
    wx.redirectTo({
      url: '/pages/originalList/original', //附带两个参数过去
    })
  },
  matNoAdd(val) {
    console.log(val)
    if (this.data.matNoList.length <= 4) {
      this.data.matNoList.push({
        matNo: val.target ? '' : val
      })
      this.setData({
        matNoList: this.data.matNoList
      });
    } else {
      wx.showToast({
        title: '钢卷号最多新增五条',
        icon: 'none',
      });
    }

  },
  matNoSearch: function (e) {
    const index = e.currentTarget.dataset.index;
    console.log(this.data.matNoList[index])
    wx.setStorageSync('matNoIndex', e.currentTarget.dataset.index);
    wx.navigateTo({
      url: `/pages/search/search?api=queryCoil&matNo=${this.data.matNoList[index].matNo}&value=${this.data.matNoList[index].matNo}`,

    })
  },
  matNoDete: function (e) {
    const index = e.currentTarget.dataset.index;
    this.data.matNoList.splice(index, 1);
    this.setData({
      matNoList: this.data.matNoList
    })

  },

})